CSCN8010 : Foundations of Machine Learning
#Import packages
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import plotly.express as px
import plotly.offline as pyo
#Plotly in notebook mode
pyo.init_notebook_mode()
The bar chart above displays the sales of different products, including laptops, smartphones, tablets, headphones, and cameras.
| Product | Sales (in units) |
|---|---|
| Laptop | 500 |
| Smartphone | 800 |
| Tablet | 300 |
| Headphones | 200 |
| Camera | 150 |
fig, ax = plt.subplots()
#Sample data
products = ['Laptop', 'Smartphone', 'Tablet', 'Headphones', 'Camera']
sales = [500, 800, 300, 200, 150]
#Bar Chart
ax.bar(products, sales)
#Customization
ax.set_ylabel('Sales (in units)')
ax.set_title('Product Sales Bar Chart')
plt.xticks(rotation=45)
plt.tight_layout()
#Display
plt.show()
The pie chart displays the distribution of monthly expenses across food, housing, transportation, and entertainment.
#Sample data
categories = ['Food', 'Housing', 'Transportation', 'Entertainment']
expenses = [500, 1000, 300, 200]
#Pie Chart
plt.figure(figsize=(8, 6))
plt.pie(expenses, labels=categories, autopct='%1.1f%%', shadow=True, startangle=140)
plt.title('Monthly Expenses')
plt.axis('equal')
#Display
plt.show()
The violin plot shows the age distribution of passengers in different classes on the Titanic.
Titanic Dataset:

#Laoding titanic dataset
titanic = sns.load_dataset('titanic')
#Violinplot
sns.set_theme(style="whitegrid")
plt.figure(figsize=(8, 6))
sns.violinplot(x='class', y='age', data=titanic, palette='Set3')
#Customization
plt.title('Age Distribution by Passenger Class')
plt.xlabel('Passenger Class')
plt.ylabel('Age')
#Display
plt.show()
The horizontal boxplot with observations shows house prices across different neighborhoods.
#Sample data
neighborhoods = ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C']
house_prices = [350000, 400000, 320000, 280000, 380000, 310000, 450000, 420000, 340000]
# Horizontal boxplot with observations using stripplot
plt.figure(figsize=(10, 6))
sns.boxplot(x=house_prices, y=neighborhoods, orient='h', palette='Set2')
sns.stripplot(x=house_prices, y=neighborhoods, orient='h', size=3, color='black', alpha=0.5)
# Customization
plt.title('House Prices by Neighborhood with Observations')
plt.xlabel('Price ($)')
plt.ylabel('Neighborhood')
#Display
plt.show()
The joint plot shows the relationship between the total bill and the tip amount in a restaurant.
Tips dataset:

#Import dataset
tips = sns.load_dataset("tips")
#Customization
sns.set_theme(style="darkgrid")
#Joinplot
g = sns.jointplot(x="total_bill", y="tip", data=tips,
kind="reg", truncate=False,
xlim=(0, 60), ylim=(0, 12),
color="m", height=7)
This dynamic scatter plot lets you explore how temperature and humidity vary in cities. Hover over points for city-specific details.
For more details, refer to the Plotly Express Documentation.
#Sample data
cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami']
temperature = [68, 75, 62, 80, 84]
humidity = [52, 62, 58, 70, 76]
#Scatter plot with hover information
fig = px.scatter(x=temperature, y=humidity, text=cities, labels={'x': 'Temperature (°F)', 'y': 'Humidity (%)'})
fig.update_traces(marker=dict(size=12),
selector=dict(mode='markers+text'))
fig.update_layout(
title='Temperature vs. Humidity',
)
#Display
fig.show()